/*
    Copyright 2011 Peter Pratscher
 
    This file is part of Surveillance.NET.

    Surveillance.NET is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Surveillance.NET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Surveillance.NET. If not, see <http://www.gnu.org/licenses/>.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Runtime.Serialization;

namespace SurveillanceDotNet.Model
{
    [Serializable]
    public class Database : ISerializable
    {
        public List<SurveillanceCamera> Cameras { get; set; }
        public List<RegularAction> RegularActions { get; set; }
        public List<AlarmAction> AlarmActions { get; set; }
        public List<MailAccount> MailAccounts { get; set; }

        public Form MainGuiForm { get; set; }

        public SurveillanceSystem SurveillanceSystem { get; private set; }
        public RemoteEMailControl RemoteEMailControl { get; private set; }

        public double AlarmTimeOutPeriod { get; set; }

        public bool EnableRemoteEMailControl { get; set; }
        public int SystemStartTimeout { get; set; }

        Database()
        {
            Cameras = new List<SurveillanceCamera>();
            RegularActions = new List<RegularAction>();
            AlarmActions = new List<AlarmAction>();
            MailAccounts = new List<MailAccount>();

            SurveillanceSystem = new SurveillanceSystem();
            RemoteEMailControl = new RemoteEMailControl();

            SystemStartTimeout = 2000;
            AlarmTimeOutPeriod = 20000;

            EnableRemoteEMailControl = false;
        }

        public static Database Instance
        {
            get
            {
                return Nested.instance;
            }
            private set
            {
                Nested.instance = value;
            }
        }

        class Nested
        {
            static Nested()
            {
            }

            internal static Database instance = new Database();
        }

        public void SaveToFile(string fileName)
        {
            BinaryFormatter formatter = new BinaryFormatter();

            using (Stream stream = new FileStream(fileName, FileMode.Create))
            {
                formatter.Serialize(stream, Database.Instance);
                stream.Close();
            }
        }

        public void LoadFromFile(string fileName, Form mainForm, System.ComponentModel.PropertyChangedEventHandler propertyChangedEventHandler)
        {
            BinaryFormatter formatter = new BinaryFormatter();

            using (Stream stream = new FileStream(fileName, FileMode.Open))
            {
                try
                {
                    Nested.instance = (Database)formatter.Deserialize(stream);
                    Nested.instance.MainGuiForm = mainForm;
                    Nested.instance.SurveillanceSystem.PropertyChanged += propertyChangedEventHandler;
                    stream.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Invalid configuration file: " + ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        protected Database(SerializationInfo info, StreamingContext context)
        {
            SurveillanceSystem = new SurveillanceSystem();
            RemoteEMailControl = (RemoteEMailControl)info.GetValue("RemoteEMailControl", typeof(RemoteEMailControl));

            Cameras = (List<SurveillanceCamera>)info.GetValue("Cameras", typeof(List<SurveillanceCamera>));
            RegularActions = (List<RegularAction>)info.GetValue("RegularActions", typeof(List<RegularAction>));
            AlarmActions = (List<AlarmAction>)info.GetValue("AlarmActions", typeof(List<AlarmAction>));
            MailAccounts = (List<MailAccount>)info.GetValue("MailAccounts", typeof(List<MailAccount>));

            EnableRemoteEMailControl = info.GetBoolean("EnableRemoteEMailControl");
            SystemStartTimeout = info.GetInt32("SystemStartTimeout");
            AlarmTimeOutPeriod = info.GetDouble("AlarmTimeOutPeriod");
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Cameras", Cameras);
            info.AddValue("RegularActions", RegularActions);
            info.AddValue("AlarmActions", AlarmActions);
            info.AddValue("MailAccounts", MailAccounts);
            info.AddValue("EnableRemoteEMailControl", EnableRemoteEMailControl);
            info.AddValue("SystemStartTimeout", SystemStartTimeout);
            info.AddValue("RemoteEMailControl", RemoteEMailControl);
            info.AddValue("AlarmTimeOutPeriod", AlarmTimeOutPeriod);
        }
    }
}
